/*
 * File:   Tokenizer.cpp
 * Author: henrya
 *
 * Created on June 13, 2009, 8:48 PM
 */

#include "Tokenizer.h"

//@pre: str != ""
Tokenizer::Tokenizer(const string& str, const string& delimiters) : strToTokenize((string&)str) {
    // Skip delimiters at beginning.
    string::size_type lastPos = strToTokenize.find_first_not_of(delimiters, 0);
    // Find first "non-delimiter".
    string::size_type pos = strToTokenize.find_first_of(delimiters, lastPos);

    while (string::npos != pos || string::npos != lastPos) {
        // Found a token, add it to the vector.
        tokens.push_back(strToTokenize.substr(lastPos, pos - lastPos));
        // Skip delimiters.  Note the "not_of"
        lastPos = strToTokenize.find_first_not_of(delimiters, pos);
        // Find next "non-delimiter"
        pos = strToTokenize.find_first_of(delimiters, lastPos);
    }
    iter=tokens.begin();
}

Tokenizer::~Tokenizer() {
}

string* Tokenizer::next() {
    string* res=peek();
    iter++;
    return res;
}

string* Tokenizer::peek() {
    string* res = &(*iter);
    if (iter == tokens.end()) {
        return NULL;
    }
    return res;
}

unsigned int Tokenizer::size() {
    return tokens.size();
}

bool Tokenizer::hasNext() {
    return iter != tokens.end();
}
